package qaz.activity;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;

import qaz.adapter.IconNewsAdapter;
import qaz.network.Connection;
import qaz.network.LoadData;
import qaz.news.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

public class NewsFeedsActivity extends Activity {
	/** Called when the activity is first created. */
	private IconNewsAdapter mIconAdapter;
	//public static String socketAddress;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);
		/*URL url;
		try {
			url = new URL("http://202.92.4.23:2024/RQ_SERVER");

			StringWriter content = new StringWriter();

			BufferedReader in = new BufferedReader(new InputStreamReader(
					url.openStream()));

			String line;
			while ((line = in.readLine()) != null) {
				content.write(line);
				socketAddress=line;
				Log.i("newfeed",line);
			}

			in.close();
			content.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		
		LoadData ld= new LoadData(this);
		ld.execute(LoadData._INIT);
		Log.i("newfeed","loaddata is running");
		GridView gr = (GridView) findViewById(R.id.gridview);
		mIconAdapter = new IconNewsAdapter(this);
		gr.setAdapter(mIconAdapter);
		gr.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Log.i("NewsTest1", "clicked at:" + arg2 + "  "
						+ IconNewsAdapter.mThumbIds[arg2]);
				Intent i = new Intent(getApplicationContext(),
						CategoryActivity.class);
				try {
					LoadData ld= new LoadData(getApplicationContext());
					ld.execute(LoadData._DATA_EXCHANGE,IconNewsAdapter.name[arg2]);					
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				// for CategoryActivity use
				//i.putExtra("INDEX", arg2);
				//startActivity(i);
			}
		});
	}
}